package org.myorg.datastraemapi.demo;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import org.myorg.datastraemapi.model.Student;

import java.util.ArrayList;
import java.util.List;

public class FlatMapDemo {
	public static void main(String[] args) throws Exception {
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		List<Student> listSource = new ArrayList<Student>() {{
			add(new Student(1, 1));
			add(new Student(2, 2));
			add(new Student(3, 3));
			add(new Student(4, 4));
			add(new Student(5, 4));
			add(new Student(6, 4));
			add(new Student(7, 4));
		}};
		DataStreamSource<Student> dataStreamSource = env.fromCollection(listSource);

		SingleOutputStreamOperator<Student> flatMap = dataStreamSource.flatMap(new FlatMapFunction<Student, Student>() {
			@Override
			public void flatMap(Student student, Collector<Student> collector) throws Exception {
				if (student.id % 2 == 0) {
					collector.collect(student);
				}
			}
		});
		flatMap.print();

		env.execute("Flink Streaming Java API Skeleton");
	}
}
